<?php

/**
 * 课程商品
 */
namespace app\avikcm\controller;
use think\Db;
use think\facade\Request;

class CourseGoods extends Common
{
    public function initialize()
    {
        parent::initialize();
    }


    // 课程商品列表
    public function list(){
        $course_id = input('course_id');
        if (Request::isAjax()) {
            $where = [];
            if($course_id){
                $where[] = ['course_id', '=', $course_id];
            }
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = db('course_goods')
                ->where($where)
                ->order('sort desc')
                ->paginate(['list_rows' => $pageSize, 'page' => $page])
                ->toArray();
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        $this->assign('course_id', $course_id);
        return $this->fetch();
    }

    /* 商品列表 */
    public function goodsList()
    {
        $course_id = input('course_id', 0);
        $key = input('key','');
        if (Request::isAjax()) {
            $where[] = ['del', '=', 0];
            if ($key) {
                $where[] = ['name', 'like', '%'. $key.'%'];
            }
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = db('goods_info')
                ->field('id,name,desc,sale_price,create_time')
                ->where($where)
                ->order('sort desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            foreach($list['data'] as $key=>$val){
                $pic = db('goods_info_pic')->where('gid', $val['id'])->find();
                $list['data'][$key]['pic'] = $pic['pic'];
            }
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        $this->assign('course_id', $course_id);
        return $this->fetch('goods_list');
    }

    // 关联商品
    public function add(){
        if(Request::isAjax()){
            //构建数组
            $data['course_id'] = input('course_id');
            $data['goods_id'] = input('goods_id');
            $exists = db('course_goods')->where(['course_id'=>$data['course_id'], 'goods_id'=>$data['goods_id']])->find();
            if(empty($exists)){
                $goods = db('goods_info')->where('id', $data['goods_id'])->find();
                $pic = db('goods_info_pic')->where('gid', $data['goods_id'])->find();
                $data['name'] = $goods['name'];
                $data['desc'] = $goods['desc'];
                $data['price'] = $goods['sale_price'];
                $data['pic'] = $pic['pic']? $pic['pic']: '';
                $data['create_time'] = date("Y-m-d H:i:s");
                db('course_goods')->insert($data);
                $result['code'] = 1;
                $result['msg'] = '关联商品成功!';
            }
            else{
                $result['code'] = 0;
                $result['msg'] = '此商品已关联，请选择其它商品!';
            }
            // $result['url'] = url('goodsList');
            return $result;
        }else{
            return $this->fetch();
        }
    }

    // 删除新闻
    public function del(){
        $id = input('id');
        db('course_goods')->where('id',$id)->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }

}